import pygame
import sys
from pygame.locals import *
from collections import deque
import random
snake = deque()
line_width = 1
speed = 1
direction = [1,0]

end_x,end_y = (15,15)
end_life = True

def _init_snake():
    global snake
    snake.clear()
    snake.extend([(1,0)])
def draw_line():
    for x in range(SIZE, HEIGHT, SIZE):
        pygame.draw.line(screen, (0,0,0), (x,0), (x, HEIGHT), line_width)
    for y in range(SIZE, WIDTH, SIZE):
        pygame.draw.line(screen, (0,0,0), (0,y), (WIDTH, y), line_width)
def bound(x, y):
    if (x >= 0 and x <= WIDTH // SIZE - 1) and (y >= 0 and y <= HEIGHT // SIZE - 1) and (x,y) not in snake:
        return True
    else:
        return False
def snake_move(snake):
    global end_life
    x = snake[0][0] + direction[0]
    y = snake[0][1] + direction[1]
    print(bound(x,y))
    if (bound(x,y)):
        next_postion = (x, y)
        if next_postion == (end_x,end_y):
            end_life = False
        snake.appendleft(next_postion)
        if end_life == True:
            snake.pop()


def draw_snake():
       for s in snake:
           x = s[0] * SIZE + line_width
           y = s[1] * SIZE + line_width
           l = SIZE - line_width*2
           pygame.draw.rect(screen, (100,100,100), (x,y,l,l),0)

def draw_end():
    l = SIZE - line_width * 2
    pygame.draw.rect(screen, (200, 200, 200), (end_x*SIZE + 1, end_y*SIZE + 1, l, l), 0)
def draw_obstacle():
    obstacle = [(3,3),(3,4),(3,5)]
    for (x,y) in obstacle:
        l = SIZE - line_width * 2
        pygame.draw.rect(screen,(0,0,0),(x * SIZE + 1, y * SIZE + 1,l,l), 0)
    

def key_event(key):
    if key[K_w]:
        direction[0] = 0
        direction[1] = -1
    if key[K_s]:
        direction[0] = 0
        direction[1] = 1
    if key[K_d]:
        direction[0] = 1
        direction[1] = 0
        
    if key[K_a]:
        direction[0] = -1
        direction[1] = 0
    if key[K_u]:
        global end_life
        end_life = False
   

pygame.init()
HEIGHT,WIDTH = 1000,1000
SCREEN_SIZE = (HEIGHT,WIDTH)
SIZE = 20
screen = pygame.display.set_mode(SCREEN_SIZE)

#set title
pygame.display.set_caption("vehicle")

clock = pygame.time.Clock()

#vehicle = Vehicle()


#USEREVENT
#EVNET = pygame.USEREVENT + 1
#pygame.time.set_timer(EVENT,1000)

#
#screen.blit()

_init_snake()
while True:
    #event
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
        #elif event.type == EVENT:
        #    pass
    
    screen.fill((5,125,150))
    
    #key
    key = pygame.key.get_pressed()
    
    #change attribute
    #draw object
    
    #screen.blit(vehicle.surf, vehicle.rect)



    #detect crash
    #crash_result = pygame.sprite.collide_rect(spirite_1, spirite_2)
    draw_line()
    key_event(key)
    print((end_x,end_y))

    snake_move(snake) 
    draw_snake()
    draw_obstacle()
    if end_life == True:
        draw_end()
    else :
        end_x = random.randint(0,WIDTH // SIZE-1)
        end_y = random.randint(0,HEIGHT // SIZE-1)
        end_life = True
    pygame.display.update()
    pygame.display.flip()
    clock.tick(10)

